package com.hll.api.controller.xinhua;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hll.api.config.web.RespMessage;
import com.hll.api.entity.Theme;
import com.hll.api.service.IThemeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 主题 前端控制器
 * </p>
 *
 * @author 何龙
 * @since 2019-07-18
 */
@RestController
@RequestMapping("/api/xinhua")
public class ThemeController {
	@Resource
	IThemeService service;

	@GetMapping("/theme/default")
	public RespMessage getTheme(){
		QueryWrapper<Theme> wrapper= new QueryWrapper<>();
		wrapper.eq("is_default",1);
		Theme one = service.getOne(wrapper);
		return RespMessage.success(one,null);
	}

	@PutMapping("/theme/set/{id}")
	public RespMessage set(@PathVariable Integer id){
		Theme theme = service.getById(id);
		if (theme==null){
			return RespMessage.error("主题不存在");
		}
		service.setTheme(theme);
		return RespMessage.success("成功将主题设置为【"+theme.getName()+"】");
	}


	@PutMapping("/theme/{id}")
	public RespMessage update(@PathVariable Integer id,Theme theme){
		if (theme==null||theme.getId()==null){
			return RespMessage.error("id不能为空");
		}
		service.updateById(theme);
		return RespMessage.success("修改成功");
	}


	@DeleteMapping("/theme/{id}")
	public RespMessage del(@PathVariable Integer id){
		boolean b = service.removeById(id);
		return RespMessage.success("删除成功");
	}


	@PostMapping("/theme")
	public RespMessage add(Theme theme){
		if (theme==null|| StringUtils.isEmpty(theme.getName())){
			return RespMessage.error("参数不正确");
		}
		theme.setIsDefault(false);
		theme.setId(null);
		service.mySave(theme);
		return RespMessage.success("增加成功");
	}

	@GetMapping("/theme")
	public RespMessage get(Integer id){
		if (id==null){
			return RespMessage.error("id不能为空");
		}
		Theme byId = service.getById(id);
		return RespMessage.success(byId,null);
	}

	@GetMapping("/theme/list")
	public RespMessage list(){
		return RespMessage.success(service.list(),null);
	}
}
